package com.mobile.centaur.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import androidx.core.view.NestedScrollingParent;
import androidx.core.view.NestedScrollingParent2;
import androidx.core.view.ViewCompat;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

public class MyNestedParentLayout extends SwipeRefreshLayout implements NestedScrollingParent2 {

    public MyNestedParentLayout(Context context) {
        super(context);
    }

    public MyNestedParentLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyNestedParentLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context);
    }

    ListView listView;
    public void setListView(ListView listView){
        this.listView=listView;
    }

    /**
     * onTouchEvent 和 dispatchTouchEvent两个事件必须同时调用才能完美解决滑动冲突
     * onTouchEvent 返回true和false 决定当前view 是否消耗
     * dispatchTouchEvent 返回true 和false 决定当前事件是否自身和子view是否分发
     *
     * listView.onTouchEvent(event) 将event事件传递给特定view后 事件就会由特定view来接管处理
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (listView!=null&&listView.getFirstVisiblePosition() == 0 && getlistFirstItemY()>=getListViewY()) {
            return super.onTouchEvent(event);
        }
        if (listView!=null){
            return listView.onTouchEvent(event);
        }
        return super.onTouchEvent(event);
    }


    /**
     * 同上
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (listView!=null&&listView.getFirstVisiblePosition() == 0&&getlistFirstItemY()>=getListViewY()) {
            return super.dispatchTouchEvent(ev);
        }else {
            if (listView!=null){
                return listView.dispatchTouchEvent(ev);
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    private int getlistFirstItemY(){
        int yCoordinate=0;
        if (listView!=null&&listView.getChildCount() > 0) {
            View firstItemView = listView.getChildAt(0);
            int[] location = new int[2];
            firstItemView.getLocationOnScreen(location);
            yCoordinate = location[1];
            // 现在 yCoordinate 就是第一个 item 的 Y 轴位置
        }
        return yCoordinate;
    }

    private int getListViewY(){
        int yCoordinate=0;
        if (listView!=null){
            int[] location = new int[2];
            listView.getLocationOnScreen(location);
            yCoordinate = location[1];
        }
        return yCoordinate;
    }

    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL)!= 0;
    }

    @Override
    public void onNestedScrollAccepted(View child, View target, int axes) {
        // 可以在这里记录一些状态或进行初始化操作
        System.out.println("Nested scroll accepted. Axes: " + axes);
    }

    @Override
    public void onStopNestedScroll(View target) {
        // 可以在这里进行一些清理操作
    }

    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        // 处理嵌套滚动过程中的滚动事件
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        // 处理嵌套滚动之前的预滚动事件
    }

    @Override
    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
        return false;
    }

    @Override
    public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
        return false;
    }

    @Override
    public int getNestedScrollAxes() {
        return 0;
    }


    /**
     *
     *
     *  xml 嵌套案例在  refres_listview.xml
     *  调用方式
     *
     *  swipeRefreshLayout.setListView(lvChuFaTask);
     *
     *         swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
     *             @Override
     *             public void onRefresh() {
     *
     *                 pullTask(false);
     *                 swipeRefreshLayout.setRefreshing(false);
     *             }
     *         });
     */
}